home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / f_reducehq.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  3.6 KB  |  107 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "filter.h"
  19.  
  20. #define READSRC(byteoff, lineoff) (*(unsigned long *)((char *)src + pitch*(lineoff) + (byteoff*4)))
  21.  
  22. extern "C" void asm_reduce2hq_run(
  23.         void *dst,
  24.         void *src,
  25.         unsigned long width,
  26.         unsigned long height,
  27.         unsigned long srcstride,
  28.         unsigned long dststride);
  29.  
  30. static int reduce2hq_run(const FilterActivation *fa, const FilterFunctions *ff) {
  31.     unsigned long w,h;
  32.     unsigned long *src = (unsigned long *)fa->src.data, *dst = (unsigned long *)fa->dst.data;
  33.     unsigned long pitch = fa->src.pitch;
  34.     unsigned long rb, g;
  35.  
  36.     if (!(fa->src.h & 1)) {
  37.         src -= pitch>>2;
  38.  
  39.         if (!(fa->src.w & 1)) {
  40.             rb    =   ((READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  41.                     +(READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  42.             g    =   ((READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  43.                     +(READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  44.             *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  45.             ++src;
  46.         }
  47.         ++src;
  48.  
  49.         w = (fa->src.w-1)/2;
  50.         do {
  51.             rb    =   ((READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff)
  52.                     +(READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  53.                     +(READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  54.             g    =   ((READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00)
  55.                     +(READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  56.                     +(READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  57.             *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  58.             src += 2;
  59.         } while(--w);
  60.  
  61.         src += (fa->src.modulo + fa->src.pitch)>>2;
  62.         dst += fa->dst.modulo>>2;
  63.     }
  64.  
  65.     asm_reduce2hq_run(
  66.         fa->src.w&1 ? dst : dst+1,
  67.         fa->src.w&1 ? src+1 : src+2,
  68.         (fa->src.w-1)/2,
  69.         (fa->src.h-1)/2,
  70.         fa->src.pitch,
  71.         fa->dst.pitch);
  72.  
  73.     if (!(fa->src.w & 1)) {
  74.         h = (fa->src.h-1)/2;
  75.         do {
  76.             rb    =   (((READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff))*2
  77.                     +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  78.             g    =   (((READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00))*2
  79.                     +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  80.             *dst = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  81.  
  82.             src += fa->src.pitch>>1;
  83.             dst += fa->dst.pitch>>2;
  84.         } while(--h);
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
  90. static long reduce2hq_param(FilterActivation *fa, const FilterFunctions *ff) {
  91.     fa->dst.w /= 2;
  92.     fa->dst.h /= 2;
  93.     fa->dst.pitch = fa->dst.w*4;
  94.  
  95.     return FILTERPARAM_SWAP_BUFFERS;
  96. }
  97.  
  98. FilterDefinition filterDef_reduce2hq={
  99.     0,0,NULL,
  100.     "2:1 reduction (high quality)",
  101.     "Reduces the size of each frame by 2:1 in both directions. A 3x3 overlapping matrix is used.\n\n[Assembly optimized] [MMX optimized]",
  102.     NULL,NULL,
  103.     0,
  104.     NULL,NULL,
  105.     reduce2hq_run,
  106.     reduce2hq_param,
  107. };